﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GTA
{
    public partial class FrmOptions : BaseForm
    {
        #region Local Objects
        private UserControls.Tools.UcAlerts ucAlerts;
        private UserControls.Tools.UcGeneral ucGeneral;
        private UserControls.Tools.UcHolidays ucHolidays;
        #endregion

        #region Starting Form
        public FrmOptions()
        {
            InitializeComponent();
        }
        private void FrmOptions_Load(object sender, EventArgs e)
        {
            StartForm();
        }
        private void StartForm()
        {
            SetChildConfigurationUserControls();
            lstOptionGroups.SelectedItems.Clear();
            lstOptionGroups.SelectedIndices.Add(0);
            ucHolidays.lstHolidays.ColumnClick += new ColumnClickEventHandler(lstHolidays_ColumnClick);
        }
        #endregion

        #region Events
        private void lstOptionGroups_SelectedIndexChanged(object sender, EventArgs e)
        {
            SetChildConfigurationVisualization();
        }
        private void cmdSave_Click(object sender, EventArgs e)
        {
            SaveConfiguration();
        }
        void lstHolidays_ColumnClick(object sender, ColumnClickEventArgs e)
        {
            SortList(ucHolidays.lstHolidays, OrdenacaoListView.Tipo.Data, ref columnOrder, ref sortOrder, e.Column);
        }
        #endregion

        #region Methods
        private void SetChildConfigurationUserControls()
        {
            ucAlerts = new GTA.UserControls.Tools.UcAlerts();
            ucGeneral = new GTA.UserControls.Tools.UcGeneral();
            ucHolidays = new GTA.UserControls.Tools.UcHolidays();
            ucAlerts.Parent = pnlComponents;
            ucGeneral.Parent = pnlComponents;
            ucHolidays.Parent = pnlComponents;
            ucAlerts.Dock = DockStyle.Fill;
            ucGeneral.Dock = DockStyle.Fill;
            ucHolidays.Dock = DockStyle.Fill;
        }
        private void SetChildConfigurationVisualization()
        {
            HideChildConfigurationUserControls();
            if (lstOptionGroups.SelectedItems.Count > 0)
            {
                switch (lstOptionGroups.SelectedIndices[0])
                {
                    case 0:
                        ucGeneral.Visible = true;
                        break;
                    case 1:
                        ucHolidays.Visible = true;
                        break;
                    case 2:
                        ucAlerts.Visible = true;
                        break;
                }
            }
        }
        private void HideChildConfigurationUserControls()
        {
            ucAlerts.Visible = false;
            ucGeneral.Visible = false;
            ucHolidays.Visible = false;
        }
        private void SaveConfiguration()
        {
            try
            {
                if (ValidateForm())
                {
                    bool informativeSummaryProjectsChanged = false;
                    bool informativeSummaryArchitectsChanged = false;
                    bool informativeSummaryStatesChanged = false;
                    bool informativeSummaryCategoriesChanged = false;
                    bool alertTimerChanged = false;

                    Program.LoadSystemConfiguration();
                    BusinessEntity.Configuration.General configGeneral = ucGeneral.GetGeneralConfiguration();

                    if (configGeneral.TimeSearchInformativeSummaryProjects.CompareTo(Program.SystemConfiguration.General.TimeSearchInformativeSummaryProjects) != 0)
                        informativeSummaryProjectsChanged = true;
                    if (configGeneral.TimeSearchInformativeSummaryArchitects.CompareTo(Program.SystemConfiguration.General.TimeSearchInformativeSummaryArchitects) != 0)
                        informativeSummaryArchitectsChanged = true;
                    if (configGeneral.TimeSearchInformativeSummaryStates.CompareTo(Program.SystemConfiguration.General.TimeSearchInformativeSummaryStates) != 0)
                        informativeSummaryStatesChanged = true;
                    if (configGeneral.TimeSearchInformativeSummaryCategories.CompareTo(Program.SystemConfiguration.General.TimeSearchInformativeSummaryCategories) != 0)
                        informativeSummaryCategoriesChanged = true;
                    if (configGeneral.TimeAlert.CompareTo(Program.SystemConfiguration.General.TimeAlert) != 0)
                        alertTimerChanged = true;

                    Program.SystemConfiguration.General = configGeneral;
                    Program.SaveSystemConfiguration();

                    BusinessLogic.Configuration bl = new GTA.BusinessLogic.Configuration();
                    Program.Holidays = ucHolidays.GetHolidaysConfiguration();
                    bl.SaveHolidays(Program.Holidays);

                    MessageBox.Show("Configurações salvas com sucesso!", "Confirmação", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    if (informativeSummaryProjectsChanged)
                        MainForm.RestartInformativeSummaryProjects();
                    if (informativeSummaryArchitectsChanged)
                        MainForm.RestartInformativeSummaryArchitects();
                    if (informativeSummaryStatesChanged)
                        MainForm.RestartInformativeSummaryStates();
                    if (informativeSummaryCategoriesChanged)
                        MainForm.RestartInformativeSummaryCategories();
                    if (alertTimerChanged)
                        MainForm.RestartAlertsProcessTimer();

                    this.Close();
                }
            }
            catch (Exception ex)
            {
                HandleException("Salvar Configurações do Sistema", ex);
            }
        }
        private bool ValidateForm()
        {
            bool valid = true;
            StringBuilder message = new StringBuilder("Informe as configurações obrigatórias!\r\n");

            valid = ucGeneral.ValidateForm();
            message.Append(ucGeneral.ValidationErrorMessage);

            if (!valid)
                MessageBox.Show(message.ToString(), "Alerta", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            return valid;
        }
        #endregion
    }
}
